module ApplicationHelper

  def icone_config_conteudo(config_conteudo)
    image_tag('icones_tipo_conteudo/' << config_conteudo.icone << '.gif', {"border"=>'0'})
  end

  def get_tree_data(tree, parent_id, arrastavel)
    ret = "<div class='outer_tree_element' >"
    tree.each do |node|
      if node.parent_id == parent_id
        node.style = (@ancestors and @ancestors.include?(node.id))? 'display:inline' : 'display:none'
        display_expanded = (@ancestors and @ancestors.include?(node.id))? 'inline' : 'none'
        display_collapsed = (@ancestors and @ancestors.include?(node.id))? 'none' : 'inline'
        ret += "<div class='inner_tree_element' id='#{node.id}_tree_div'>"
        if node.children_count > 0
          ret += "<img id='#{node.id.to_s}expanded' src='/images/expanded.gif' onclick='javascript: return toggleMyTree(\"#{node.id}\"); ' style='display:#{display_expanded}; cursor:pointer;'  />  "
          ret += "<img style='display:#{display_collapsed}; cursor:pointer;'  id='#{node.id.to_s}collapsed' src='/images/collapsed.gif' onclick='javascript: return toggleMyTree(\"#{node.id.to_s}\"); '  />  "
        end
        if arrastavel
          ret += " <img src='/images/drag.gif' style='cursor:move' id='#{node.id}_drag_image' align='absmiddle' class='drag_image' /> "
        end

        ret += "<span id='#{node.id}_tree_item'>"
        ret += yield node
        ret += "</span>"
        ret += "<span id='#{node.id}children' style='#{node.style}' >"
        ret += get_tree_data(node.children, node.id, arrastavel){|n| yield n}
        ret += "</span>"
        ret += "</div>"
      end
    end
    ret += "</div>"
    return ret
  end

  def bloco(nome)
    ret = ""
    if(nome.nil?)
      ret = 'NÃO FOI PASSADO O NOME DO BLOCO'
    else
      bloco = Bloco.find_by_nome(nome)
      if bloco
        ret= render_component(
               :controller=>bloco.bloco_tipo.controller,
               :action=>bloco.bloco_tipo.action
             )
      else
        ret = 'O BLOCO N�O EXISTE'
      end
    end
    ret
  end

  def grupo_de_blocos(nome)
    ret = ""
    grupo = BlocoGrupo.find_by_nome(nome)
    grupo.bloco.each do |bloco|
      ret << render_component(
               :controller=>bloco.bloco_tipo.controller,
               :action=>bloco.bloco_tipo.action
             )
    end
    ret
  end

  def sobe_desce_geral(record,controller)
    html = ""
    if(!record.last?)
      img = "<img border=0 src='/images/setas_ordenacao/desce.png' title='desce'/>"
      html = link_to_remote(
             img,
             :url=>{
                :controller=>controller,
                :action=>:desce,
                :id=>record.id
             })
    end
    if(!record.first?)
      img = "<img border=0 src='/images/setas_ordenacao/sobe.png' title='sobe'/>"
      html << " "+link_to_remote(
             img,
             :url=>{
                :controller=>controller,
                :action=>:sobe,
                :id=>record.id
             })
    end
    html
  end
end
